package com.khalil.kkadmin.ai.rag.knowledgeset;

import com.alibaba.fastjson2.JSON;
import com.khalil.kkadmin.ai.rag.knowledgeset.dto.RagKnowledgeDocDto;
import com.khalil.kkadmin.ai.rag.knowledgeset.service.RagKnowledgeDelegateService;
import com.khalil.kkadmin.ai.rag.knowledgeset.service.RagKnowledgeDocService;
import com.khalil.kkadmin.common.util.StrUtil;
import com.khalil.kkadmin.common.web.ReqBody;
import com.khalil.kkadmin.common.web.RespResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("/rag/knowledgeDoc")
public class RagKnowledgeDocController {

    @Autowired
    private RagKnowledgeDocService ragKnowledgeDocService;

    @Autowired
    private RagKnowledgeDelegateService ragKnowledgeDelegateService;

    @PostMapping("/upload")
    public RespResult<List<RagKnowledgeDocDto>> upload(@RequestParam("file") List<MultipartFile> files, @RequestParam("knowledgeSetId") Long knowledgeSetId) {
        return RespResult.ok(ragKnowledgeDocService.upload(knowledgeSetId, files));
    }

    @GetMapping("/list")
    public RespResult<List<RagKnowledgeDocDto>> getDocList(@RequestParam("knowledgeSetId") Long knowledgeSetId) {
        List<RagKnowledgeDocDto> list = ragKnowledgeDocService.listByKnowledgeSetId(knowledgeSetId);
        return RespResult.ok(list);
    }

    @PostMapping("/multiDelete")
    public void deleteDoc(@RequestBody ReqBody<String> reqBody) {
        String data = reqBody.getData();
        if (StrUtil.isEmpty(data)) {
            return;
        }
        List<Long> idList = JSON.parseArray(data, Long.class);
        ragKnowledgeDocService.deleteByIdList(idList);
    }

    @PostMapping("documentETL")
    public void documentETL(@RequestBody ReqBody<Long> reqBody) {
        Long id = reqBody.getData();
        ragKnowledgeDelegateService.documentETL(id);
    }
}
